void ((function() {
	var baseUrl = "http://10.131.252.41:8080/edu.fudan.ss.wml.marker.test";
	var le = document.createElement("link");
	le.setAttribute("rel", "stylesheet");
	le.setAttribute("type", "text/css");
	le.setAttribute("href", baseUrl + "/css/style.css");
	document.body.appendChild(le);
	var baseUrl = "http://10.131.252.41:8080/edu.fudan.ss.wml.marker.test";
	var le = document.createElement("link");
	le.setAttribute("rel", "stylesheet");
	le.setAttribute("type", "text/css");
	le.setAttribute("href", baseUrl + "/css/comment.css");
	document.body.appendChild(le);

	var e2 = document.createElement('script');
	e2.setAttribute('language', 'javascript');
	e2.setAttribute('src', baseUrl + '/script/tools.js');
	document.body.appendChild(e2);
	var e2 = document.createElement('script');
	e2.setAttribute('language', 'javascript');
	e2.setAttribute('src', baseUrl + '/script/comment.js');
	document.body.appendChild(e2);
	var e2 = document.createElement('script');
	e2.setAttribute('language', 'javascript');
	e2.setAttribute('src', baseUrl + '/script/colorPanel.js');
	document.body.appendChild(e2);
	var id = 'divmenu';
	var draggableDiv = document.createElement('div');
	draggableDiv.setAttribute('id', id);
	draggableDiv.setAttribute('contentEditable', false);
	draggableDiv.style.top = "50px";
	draggableDiv.style.right = "50px";
	document.body.appendChild(draggableDiv);
	drag(id, 1);
	prepareButtons(baseUrl);
	var addr = location.href;
	markit_oldMarkData = {
		pageId : -1,
		data : [],
	};
	markit_markData = {
		pageId : -1,
		data : [],
		cmnts: null
	};
	requestMarkData(addr);
})());
function drag(o, s) {
	var isChrome = navigator.userAgent.toLowerCase().indexOf("chrome") > 0;
	if (typeof o == "string")
		o = document.getElementById(o);
	o.orig_x = parseInt(o.style.left) - document.body.scrollLeft;
	o.orig_y = parseInt(o.style.top) - document.body.scrollTop;
	o.onmousedown = function(a) {
		this.style.cursor = "move";
		this.style.zIndex = 9997;
		var d = document;
		colorClose();
		if (!a)
			a = window.event;
		var sl = isChrome ? 0 : d.body.scrollLeft;
		var st = isChrome ? 0 : d.body.scrollTop;
		var x = a.clientX + sl - o.offsetLeft;
		var y = a.clientY + st - o.offsetTop;
		if (o.setCapture)
			o.setCapture();
		else if (window.captureEvents)
			window.captureEvents(Event.MOUSEMOVE | Event.MOUSEUP);
		o.onmousemove = function(a) {
			if (!a)
				a = window.event;
			var sl = isChrome ? 0 : document.body.scrollLeft;
			var st = isChrome ? 0 : document.body.scrollTop;
			o.style.left = a.clientX + sl - x + "px";
			o.style.top = a.clientY + st - y + "px";
			o.orig_x = parseInt(o.style.left) - sl;
			o.orig_y = parseInt(o.style.top) - st;
		};
		o.onmouseup = function() {
			if (o.releaseCapture)
				o.releaseCapture();
			else if (window.captureEvents)
				window.captureEvents(Event.MOUSEMOVE | Event.MOUSEUP);
			o.onmousemove = null;
			o.onmouseup = null;
			o.ondragstart = null;
			o.onselectstart = null;
			o.onselect = null;
			o.style.cursor = "pointer";
			o.style.zIndex = o.orig_index;
		};
	};
	if (s) {
		var orig_scroll = window.onscroll ? window.onscroll : function() {
		};
		window.onscroll = function() {
			orig_scroll();
			var sl = isChrome ? 0 : document.body.scrollLeft;
			var st = isChrome ? 0 : document.body.scrollTop;
			o.style.left = o.orig_x + sl + "px";
			o.style.top = o.orig_y + st + "px";
		};
	}
}
function prepareButtons(baseUrl) {
	var draggableDiv = document.getElementById("divmenu");
	var src = baseUrl + "/icon/Bold.gif";
	var b1 = createCtlBtn("markBtn", "idBold",
			"changeStyle('Bold',false,null,1)", src);
	draggableDiv.appendChild(b1);
	src = baseUrl + "/icon/Italic.gif";
	b1 = createCtlBtn("markBtn", "idItalic",
			"changeStyle('Italic',false,null,1)", src);
	draggableDiv.appendChild(b1);
	src = baseUrl + "/icon/Underline.gif";
	b1 = createCtlBtn("markBtn", "idUnderline",
			"changeStyle('Underline',false,null,1)", src);
	draggableDiv.appendChild(b1);
	src = baseUrl + "/icon/StrikeThrough.gif";
	b1 = createCtlBtn("markBtn", "idStrikeThrough",
			"changeStyle('StrikeThrough',false,null,1)", src);
	draggableDiv.appendChild(b1);
	src = baseUrl + "/icon/Quote.gif";
	b1 = createCtlBtn("markBtn", "idQuote",
			"changeStyle('formatblock',false,'blockquote',1)", src);
	draggableDiv.appendChild(b1);
	src = baseUrl + "/icon/Comment.png";
	b1 = createCtlBtn("markBtn", "idComment", "commentIt(1)", src);
	draggableDiv.appendChild(b1);
	// src=baseUrl+"/icon/Redo.gif";
	// b1 =
	// createCtlBtn("markBtn","idRedo","changeStyle('Redo',false,null)",src);
	// draggableDiv.appendChild(b1);
	// src=baseUrl+"/icon/Undo.gif";
	// b1 =
	// createCtlBtn("markBtn","idUndo","changeStyle('Undo',false,null)",src);
	// draggableDiv.appendChild(b1);
	src = baseUrl + "/icon/ForeColor.gif";
	b1 = createCtlBtn("markBtn", "idForeColor", "colorOpen(event,1)", src);
	draggableDiv.appendChild(b1);
	src = baseUrl + "/icon/BackColor.gif";
	b1 = createCtlBtn("markBtn", "idBackColor", "colorOpen(event,2)", src);
	draggableDiv.appendChild(b1);
	src = baseUrl + "/icon/submit.png";
	b1 = createCtlBtn("markBtn", "idSubmit", "submitMarkData()", src);
	draggableDiv.appendChild(b1);
}
function createCtlBtn(clazz, id, onclick, img) {
	var b = document.createElement("input");
	b.setAttribute("type", "image");
	b.setAttribute("class", clazz);
	b.setAttribute("id", id);
	b.setAttribute("onclick", onclick);
	b.setAttribute("src", img);
	return b;
}
function createAjax() {
	var xmlHttp;
	try {
		xmlHttp = new XMLHttpRequest();
	} catch (e) {
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				alert("Your browser does not support AJAX!");
				xmlHttp = false;
			}
		}
	}
	return xmlHttp; // 返回XMLHttp实例
}

function onSubmitted() {
	if (this.readyState == 4) {
		if (this.status == 200) {
			console.log(this.responseText);
			if (this.responseText.indexOf("true") > -1) {
				markit_oldMarkData.data.push(markit_markData.data);
				markit_oldComments.addAll(markit_comments);
				markit_comments.clear();
				markit_markData.data = [];
			}
		}
	}
}

function onMarkData() {
	if (this.readyState == 4) {
		if (this.status == 200) {
			var data = JSON.parse(this.responseText);
			markit_markData.pageId = data.pageId;
			markit_oldComments.container = data.cmnts;
			var i = 0;
			for (; i < data.data.length; i++) {
				findit(data.data[i].txt);
				if (!markit_found)
					findprev(data.data[i].txt);
				console.log("found?("+markit_found+") type?("+data.data[i].tp+") "+data.data[i].txt);
				if(data.data[i].tp=="Comment"){
					var index = data.cmnts.v;
					if(commentCount<index)
						commentCount = index;
					commentIt(0,index);
				}
				changeStyle(data.data[i].tp, false, data.data[i].v, 0);
			}
		}
	}
}
var markit_charset = document.charset || document.characterSet;
function requestMarkData(local) {
	var url = "http://10.131.252.41:8080/edu.fudan.ss.wml.marker.test/Request.jsp";
	var xmlHttp = createAjax();// /创建xmlHttpRequest组件
	xmlHttp.onreadystatechange = onMarkData;// /设置回调函数,onreadystatechange是方法,状态发生改变时调用haolejiaowo方法
	xmlHttp.open("post", url, true);// /初始化xmlHttpRequest组件,GET提交,true表示请求是异步请求
	xmlHttp.setRequestHeader("Content-Type",
			"application/x-www-form-urlencoded;charset=utf-8");
	xmlHttp.send(JSON.stringify({
		'local' : local
	}));// /发送请求
}
function submitMarkData() {
	var url = "http://10.131.252.41:8080/edu.fudan.ss.wml.marker.test/Submit.jsp";
	var xmlHttp = createAjax();// /创建xmlHttpRequest组件
	xmlHttp.onreadystatechange = onSubmitted;// /设置回调函数,onreadystatechange是方法,状态发生改变时调用haolejiaowo方法
	xmlHttp.open("post", url, true);// /初始化xmlHttpRequest组件,GET提交,true表示请求是异步请求
	xmlHttp.setRequestHeader("Content-Type",
			"application/x-www-form-urlencoded;charset=utf-8");
	markit_markData.cmnts = markit_comments.container;
	xmlHttp.send(JSON.stringify(markit_markData));// /发送请求
}

function changeStyle(type, b, e, isNew) {
	var txt = getSelectionText();
	if (txt == "" || txt == undefined)
		return;
	if(type!="Comment"){
		document.body.setAttribute("contentEditable", true);
		document.execCommand(type, b, e);
		document.body.setAttribute("contentEditable", false);
	}
	clearSelection();
	var mark = {
		'tp' : type,
		'txt' : txt,
		'v' : e
	};
	if (isNew == 1)
		markit_markData.data.push(mark);
	else
		markit_oldMarkData.data.push(mark);
}

function clearSelection () {
	if (window.getSelection)
		window.getSelection().removeAllRanges();
	else if (document.selection)
		document.selection.empty();
}

function getSelectionText() {
	if(window.getSelection) {
		return window.getSelection().toString();
	} else if(document.selection && document.selection.createRange) {
		return document.selection.createRange().text;
	}
	return '';
}

var markit_ie = (document.all);
var markit_sel; // Selection object needed for Firefox
var markit_range; // range object needed for Firefox
var markit_found = false;
var markit_finds = 0;
var markit_txt, markit_bookmark;
function findit(string) {
	if (markit_ie) {
		if (markit_txt == null) {
			markit_txt = document.body.createTextRange();
			markit_bookmark = new Array();
		}
		markit_bookmark[markit_finds] = markit_txt.getBookmark();
		if (string) // only call findText if there is a string or IE will have
			if (markit_found = markit_txt.findText(string)) // if found
			{
				markit_txt.select();
				markit_txt.scrollIntoView();
				markit_txt.collapse(false);
			} else {
				markit_finds--;
			}
	} else // Netscape or firefox
	{
		if (markit_finds > 0) {
			markit_sel = window.getSelection(); // get selection
			if (markit_sel.rangeCount > 0)
				markit_sel.removeAllRanges();
			markit_sel.addRange(markit_range);
		}
		if (string != "")
			markit_found = window.find(string, false, false);
		markit_sel = window.getSelection(); // get selection
		markit_range = markit_sel.getRangeAt(0); // get object
	}
	markit_finds++;
}

function findprev(string) {
	if (markit_ie) {
		if (markit_txt == null) {
			markit_txt = document.body.createTextRange();
			markit_bookmark = new Array();
		}
		if (markit_finds < 2) {
			markit_found = false;
			return;
		}
		markit_finds = markit_finds - 2;
		markit_txt.moveToBookmark(markit_bookmark[markit_finds]);
		findit(string);
	} else {
		if (markit_finds > 0) {
			markit_sel = window.getSelection(); // get selection
			if (markit_sel.rangeCount > 0)
				markit_sel.removeAllRanges();
			markit_sel.addRange(markit_range);
		}
		if (string != "")
			markit_found = window.find(string, false, true);
		markit_sel = window.getSelection(); // get selection
		markit_range = markit_sel.getRangeAt(0); // get object
	}
}
